{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-02T06:31:48.924622Z",
     "start_time": "2025-09-02T06:31:48.083958Z"
    }
   },
   "source": "from sklearn.neighbors import KNeighborsRegressor",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T06:31:48.929651Z",
     "start_time": "2025-09-02T06:31:48.924622Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#准备数据\n",
    "X = [[2,1],[3,1],[1,4],[2,6]]\n",
    "Y = [0.5,0.33,4,3]"
   ],
   "id": "2768c0b976a109eb",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T06:31:48.939357Z",
     "start_time": "2025-09-02T06:31:48.930948Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#kNN回归模型\n",
    "knn = KNeighborsRegressor(n_neighbors=2,weights='distance')\n",
    "knn.fit(X,Y)\n",
    "#预测\n",
    "x = [[4,9]]\n",
    "x_pred = knn.predict(x)\n",
    "print(x_pred)"
   ],
   "id": "b3ad40a3cde1e7c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.38208553]\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
